import 'package:f_enjoy_plus/core/constants/app_colors.dart';
import 'package:f_enjoy_plus/components/Header.dart';
import 'package:f_enjoy_plus/utils/http.dart';
import 'package:f_enjoy_plus/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map<String, dynamic> notice = {};

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final args = ModalRoute.of(context)?.settings.arguments;
    final params = args != null ? args as Map<String, dynamic> : {};
    if (params.isNotEmpty) {
      _getNotice(params['id']);
    } else {
      ToastUtils.error('公告不存在');
      Navigator.pop(context);
    }
  }

  /// 获取公告详情
  Future<void> _getNotice(String id) async {
    final res = await http.get('/announcement/$id');
    setState(() {
      notice = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Header(title: '公告详情'),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(16),
        child: Column(
          children: [
            Row(
              children: [
                Text(
                  notice['title'] ?? '',
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
                Spacer(),
                Text(
                  notice['createdAt'] ?? '',
                  style: TextStyle(
                    fontSize: 12,
                    color: AppColors.textSecondary,
                  ),
                ),
              ],
            ),
            Html(data: notice['content'] ?? ''),
          ],
        ),
      ),
    );
  }
}
